<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="600px"
    @close="close"
  >
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="适用业务类" prop="BusinessClassID">
        <el-select v-model="form.BusinessClassID" style="width: 240px">
          <el-option
            :label="form.BusinessClassName"
            :value="form.BusinessClassID"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属组织" prop="CompanyID">
        <el-cascader
          v-model="form.CompanyID"
          :options="CompanyOptions"
          :props="companyProps"
          clearable
          @change="getDeptment"
        />
      </el-form-item>
      <el-form-item label="所属部门" prop="DepartmentID">
        <el-cascader
          v-model="form.DepartmentID"
          :options="DeptmentOptions"
          :props="deptmentProps"
          clearable
          @change="getStaff"
        />
      </el-form-item>
      <el-form-item label="选择部门人员" prop="StaffID">
        <el-cascader
          v-model="form.StaffID"
          :options="StaffOptions"
          :props="StaffProps"
          clearable
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save()">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doSetActStaff } from '@/api/systemsetBusinessFlowset'
  import {
    getCompanySelect,
    getDeptmentSelect,
    getStaffPostRoleSelect,
    getStaffPostSelect,
    getStaffSelect,
  } from '@/api/common'

  export default defineComponent({
    name: 'SetActStaff',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        adminCompany: 0,
        formRef: null,
        form: {
          BusinessClassID: 0,
        },
        rules: {
          title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        },
        title: '',
        dialogFormVisible: false,
        actfield: '',
        actvalue: [],
        companyProps: {
          multiple: true,
          checkStrictly: true,
        },
        deptmentProps: {
          multiple: true,
          checkStrictly: true,
        },
        StaffPostProps: {
          multiple: true,
        },
        StaffProps: {
          multiple: true,
        },
        CompanyOptions: [],
        DeptmentOptions: [],
        StaffPostRoleOptions: [],
        StaffPostOptions: [],
        StaffOptions: [],
      })

      const showEdit = (row, act) => {
        state.title = '编辑'
        state.adminCompany = row.CompanyID
        state.form = Object.assign({}, row)
        state.actfield = act
        getDeptment()
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          roles: [],
        }
        state.dialogFormVisible = false
      }
      const save = () => {
        state.form[state.actfield] = state.form.StaffID
        state.form.actfield = state.actfield
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doSetActStaff(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      const getDeptment = async () => {
        console.log('form.Company', state.form.CompanyID)
        const getParams = {
          Company: state.form.CompanyID,
        }
        const {
          data: { DeptmentOptions },
        } = await getDeptmentSelect(getParams)
        state.DeptmentOptions = DeptmentOptions
      }

      const getStaffPost = async () => {
        console.log('form.Company', state.form.CompanyID)
        const getParams = {
          Company: state.form.CompanyID,
          Deptment: state.form.DepartmentID,
          StaffPostRole: state.form.StaffPostRoleID,
        }
        const {
          data: { StaffPostOptions },
        } = await getStaffPostSelect(getParams)
        state.StaffPostOptions = StaffPostOptions
      }

      const getStaff = async () => {
        const getParams = {
          Company: state.form.CompanyID,
          Deptment: state.form.DepartmentID,
        }
        const {
          data: { StaffOptions },
        } = await getStaffSelect(getParams)
        state.StaffOptions = StaffOptions
      }

      const getOptions = async () => {
        state.listLoading = true
        const {
          data: { CompanyOptions },
        } = await getCompanySelect()
        const {
          data: { StaffPostRoleOptions },
        } = await getStaffPostRoleSelect()
        state.CompanyOptions = CompanyOptions
        state.StaffPostRoleOptions = StaffPostRoleOptions
        state.listLoading = false
      }

      onMounted(() => {
        getOptions()
      })

      return {
        ...toRefs(state),
        showEdit,
        getDeptment,
        getStaffPost,
        getStaff,
        close,
        save,
      }
    },
  })
</script>
